﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;

namespace LenderClearingHouse
{
  public class Authentication
  {
    public static User ValidateUser(string username, string password)
    {
      //Get the sql connection string from Utilities Class
      SqlConnection myConnection = Utilities.GetConnection();

      // define the command query
      string sql = "SELECT * from UserAccounts where email ='" + username + "' and password='" + password + "' and is_active=1 and is_deleted=0;";

      try
      {
        myConnection.Open();
        SqlCommand cmd = myConnection.CreateCommand(); //Builds cmd obj from myConnection
        cmd.CommandText = sql;	//tells what kind of command Text or Procedure

        DataTable dt = new DataTable(); //Create datatable
        SqlDataAdapter adap = new SqlDataAdapter(cmd);
        adap.Fill(dt); //Fill datatable using SqlDataAdapter

        if (dt.Rows.Count == 1)
        {
          DataRow dr = dt.Rows[0];
          User usr = new User(dr["UserId"].ToString(), username);
          //usr.IsAdmin = Convert.ToBoolean(dr["IsAdmin"]);

          usr.FName = dr["FName"].ToString();
          usr.LName = dr["LName"].ToString();

          string role = dr["LvreoRole"].ToString();
          if (role == "NA") usr.LvreoRole = RoleAccess.NoAccess;
          else if (role == "RO") usr.LvreoRole = RoleAccess.ReadOnly;
          else if (role == "RW") usr.LvreoRole = RoleAccess.ReadWrite;

          role = dr["SdreoRole"].ToString();
          if (role == "NA") usr.SdreoRole = RoleAccess.NoAccess;
          else if (role == "RO") usr.SdreoRole = RoleAccess.ReadOnly;
          else if (role == "RW") usr.SdreoRole = RoleAccess.ReadWrite;

          role = dr["MasterlistRole"].ToString();
          if (role == "NA") usr.MasterlistRole = RoleAccess.NoAccess;
          else if (role == "RO") usr.MasterlistRole = RoleAccess.ReadOnly;
          else if (role == "RW") usr.MasterlistRole = RoleAccess.ReadWrite;

          string userRole = dr["Role"].ToString();
          if (userRole == "SA") usr.UserRole = UserRole.SuperAdmin;
          else if (userRole == "IA") usr.UserRole = UserRole.InvestorAdmin;
          else if (userRole == "IU") usr.UserRole = UserRole.InvestorUser;
          else if (userRole == "US") usr.UserRole = UserRole.User;
          else if (userRole == "EU") usr.UserRole = UserRole.EscrowUser;
          else if (userRole == "PM") usr.UserRole = UserRole.ProjectManager;

          string subRole = dr["SubRole"].ToString();
          if (subRole == "N") usr.SubRole = UserSubRole.None;
          else if (subRole == "L") usr.SubRole = UserSubRole.LVReo;
          else if (subRole == "S") usr.SubRole = UserSubRole.SDReo;

          if(!dr.IsNull("CreatedByUser")) usr.CreatedByUser = (int)dr["CreatedByUser"];

          return usr;
        }
        else throw new Exception("Invalid user name or password");
      }
      catch
      {
        myConnection.Close();
        throw;
      }
    }//End Validate User
  }

  public enum RoleAccess
  {
    NoAccess,
    ReadOnly,
    ReadWrite
  }

  public enum UserRole
  {
    SuperAdmin,
    InvestorAdmin,
    InvestorUser,
    User,
    EscrowUser,
    ProjectManager
  }

  public enum UserSubRole
  {
    LVReo,
    SDReo,
    None
  }

  public class User
  {
    protected string userId;
    protected string userName;
    RoleAccess lvreoRole = RoleAccess.NoAccess;
    RoleAccess sdreoRole = RoleAccess.NoAccess;
    bool isAdmin = false;    

    //Set Username and access level
    public User(string id, string u)
    {
      //Set variables
      this.userId = id;
      this.userName = u;
    }

    public int CreatedByUser = -1;

    public string FName;

    public string LName;

    UserRole userRole = UserRole.User;

    public UserRole UserRole
    {
      get { return userRole; }
      set { userRole = value; }
    }

    UserSubRole subRole = UserSubRole.None;

    public UserSubRole SubRole
    {
      get { return subRole; }
      set { subRole = value; }
    }

    //public bool IsAdmin
    //{
    //  get { return isAdmin; }
    //  set { isAdmin = value; }
    //}

    public RoleAccess SdreoRole
    {
      get { return sdreoRole; }
      set { sdreoRole = value; }
    }

    public RoleAccess LvreoRole
    {
      get { return lvreoRole; }
      set { lvreoRole = value; }
    }

    RoleAccess masterlistRole = RoleAccess.NoAccess;

    public RoleAccess MasterlistRole
    {
      get { return masterlistRole; }
      set { masterlistRole = value; }
    }

    //Returns the userId
    public string UserId
    {
      get
      {
        return this.userId;
      }
      set
      {
        this.userId = value;
      }
    }

    //Returns the userName
    public string UserName
    {
      get
      {
        return this.userName;
      }
      set
      {
        this.userName = value;
      }
    }
  }//End User Class
}